package com.aidex.biz.controller;

import java.math.BigDecimal;
import java.util.*;

import com.aidex.biz.domain.bo.DeptTargetSetBo;
import com.aidex.biz.domain.bo.TarGetInfoBo;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;

import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.DeptTarget;
import com.aidex.biz.service.DeptTargetService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 业绩目标设置Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2024-09-24
 */
@RestController
@RequestMapping("/biz/bizDeptTarget")
public class DeptTargetController extends BaseController {
    @Autowired
    private DeptTargetService deptTargetService;

    /**
     * 查询业绩目标设置列表
     */
    @GetMapping("/list")
    public R list(DeptTarget deptTarget, HttpServletRequest request, HttpServletResponse response) {
//        deptTarget.setDeptId(SecurityUtils.getDeptId());
        List<DeptTarget> deptTargetList = deptTargetService.findList(deptTarget);
        List<DeptTargetSetBo> deptTargetSetBos = new ArrayList<>();

        DeptTargetSetBo d = new DeptTargetSetBo();
        d.setDeptMuBiao("日");
        DeptTargetSetBo w = new DeptTargetSetBo();
        w.setDeptMuBiao("周");
        DeptTargetSetBo m = new DeptTargetSetBo();
        m.setDeptMuBiao("月");
        if (deptTargetList.size() > 0) {
            d.setId(deptTargetList.get(0).getId());
            d.setCalls(deptTargetList.get(0).getdCalls());
            d.setGenjin(deptTargetList.get(0).getdGenjins());
            d.setNewCus(deptTargetList.get(0).getdAddCus());
            d.setJinjian(deptTargetList.get(0).getdJinjians());
            d.setToHome(deptTargetList.get(0).getdGohome());
            d.setShouru(deptTargetList.get(0).getdShouru());
            w.setId(deptTargetList.get(0).getId());
            w.setCalls(deptTargetList.get(0).getwCalls());
            w.setGenjin(deptTargetList.get(0).getwGenjins());
            w.setNewCus(deptTargetList.get(0).getwAddCus());
            w.setJinjian(deptTargetList.get(0).getwJinjians());
            w.setToHome(deptTargetList.get(0).getwGohome());
            w.setShouru(deptTargetList.get(0).getwShouru());
            m.setId(deptTargetList.get(0).getId());
            m.setCalls(deptTargetList.get(0).getmCalls());
            m.setGenjin(deptTargetList.get(0).getmGenjins());
            m.setNewCus(deptTargetList.get(0).getmAddCus());
            m.setJinjian(deptTargetList.get(0).getmJinjians());
            m.setToHome(deptTargetList.get(0).getmGohome());
            m.setShouru(deptTargetList.get(0).getmShouru());
        } else {
            d.setId("no");
            d.setCalls(0L);
            d.setGenjin(0L);
            d.setNewCus(0L);
            d.setJinjian(0L);
            d.setToHome(0L);
            d.setShouru(new BigDecimal(0));
            w.setId("no");
            w.setCalls(0L);
            w.setGenjin(0L);
            w.setNewCus(0L);
            w.setJinjian(0L);
            w.setToHome(0L);
            w.setShouru(new BigDecimal(0));
            m.setId("no");
            m.setCalls(0L);
            m.setGenjin(0L);
            m.setNewCus(0L);
            m.setJinjian(0L);
            m.setToHome(0L);
            m.setShouru(new BigDecimal(0));
        }
        deptTargetSetBos.add(d);
        deptTargetSetBos.add(w);
        deptTargetSetBos.add(m);

        return R.data(deptTargetSetBos);
    }

    /**
     * 获取业绩目标设置详细信息
     */
    @GetMapping(value = "/{id}")
    public R<DeptTarget> detail(@PathVariable("id") String id) {
        return R.data(deptTargetService.get(id));
    }

    /**
     * 新增业绩目标设置
     */
    @Log(title = "业绩目标设置", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated DeptTarget deptTarget) {
        if ("no".equals(deptTarget.getId())) {
            deptTarget.setIsNewRecord();
            deptTarget.setId(UUID.randomUUID().toString());
        }
        return R.status(deptTargetService.save(deptTarget));
    }

    /**
     * 修改业绩目标设置
     */
    @Log(title = "业绩目标设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated DeptTarget deptTarget) {

        return R.status(deptTargetService.save(deptTarget));
    }


    /**
     * 删除业绩目标设置
     */
    @PreAuthorize("@ss.hasPermi('biz:bizDeptTarget:remove')")
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(deptTargetService.deleteDeptTargetByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(deptTargetService.findMaxSort(new DeptTarget()));
    }

    /**
     * 导出业绩目标设置列表
     */
    @Log(title = "业绩目标设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(DeptTarget deptTarget) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            deptTarget.setCompanyId(null);
        } else {
            deptTarget.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<DeptTarget> list = deptTargetService.findList(deptTarget);
        ExcelUtil<DeptTarget> util = new ExcelUtil<DeptTarget>(DeptTarget.class);
        return util.exportExcel(list, "业绩目标设置数据");
    }

    /**
     * 查询首页业绩目标
     *
     * @return
     */
    @GetMapping("/getIndexTarGetInfo")
    public R getIndexTarGetInfo(TarGetInfoBo tarGetInfoBo) {

        if (null == tarGetInfoBo.getDeptId()) {
            tarGetInfoBo.setDeptId(SecurityUtils.getDeptId());
        }

        DeptTarget deptTarget = new DeptTarget();
        deptTarget.setDeptId(tarGetInfoBo.getDeptId());
        deptTarget.setUserId(tarGetInfoBo.getUserId());
        List<DeptTarget> deptTargetList = deptTargetService.findList(deptTarget);

        String sql = "DATE_FORMAT(a.create_time,'%y%m') = DATE_FORMAT(now(),'%y%m')";//当前月
        switch (tarGetInfoBo.getKpiType()) {
            case "day":
                sql = "DATE(a.create_time) = CURDATE()";//当天
                break;
            case "week":
                sql = "YEAR(a.create_time) = YEAR(CURDATE()) AND WEEK(a.create_time) = WEEK(CURDATE())";//当前周
                break;
            case "month":
                sql = "DATE_FORMAT(a.create_time,'%y%m') = DATE_FORMAT(now(),'%y%m')";//当前月
                break;
        }
        tarGetInfoBo.setDateRangeSql(sql);
        deptTarget = deptTargetService.getIndexDeptTarGet(tarGetInfoBo);
        Map<String, DeptTarget> deptTargetMap = new HashMap<>();
        deptTargetMap.put("getDeptTarget", deptTarget);
        deptTargetMap.put("deptTarget", deptTargetList.size() > 0 ? deptTargetList.get(0) : new DeptTarget());
        return R.data(deptTargetMap);
    }
}
